// subPages/glucometer/index/index.js
const app = getApp()
var jsonData = require('../../../data/booldSugarJson');
var util =  require('../../../utils/util.js');
var template = require('../../../template/template.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnTxt:{
      btnTxt:'记录'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      date: util.setTime(new Date())
    })
    template.SugarTabbar("tabBar", 0, this) //0表示第一个tabbar
    this.getBloodSugarData();
  },

  //获得血糖数据
  getBloodSugarData:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/blood',
      data: {
        userId: app.globalData.userId
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          that.BloodSugarData(res.data.data)
        }else{
          that.setData({
            bloodSugarDataList: jsonData.bloodSugarDataList
          })
        }
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //处理数据
  BloodSugarData:function(bloodSugarDataList){
    var that = this
    var bloodSugarList = jsonData.bloodSugarDataList
    for(var i in bloodSugarDataList)
    {
      var item = bloodSugarDataList[i]
      if(item.type == 1)
      {
        bloodSugarList[0].blood = item.blood;
        bloodSugarList[0].status = item.status;
      }
      if(item.type == 2)
      {
        bloodSugarList[1].blood = item.blood;
        bloodSugarList[1].status = item.status;
      }
      if(item.type == 3)
      {
        bloodSugarList[2].blood = item.blood;
        bloodSugarList[2].status = item.status;
      }
      if(item.type == 4)
      {
        bloodSugarList[3].blood = item.blood;
        bloodSugarList[3].status = item.status;
      }
      if(item.type == 5)
      {
        bloodSugarList[4].blood = item.blood;
        bloodSugarList[4].status = item.status;
      }
      if(item.type == 6)
      {
        bloodSugarList[5].blood = item.blood;
        bloodSugarList[5].status = item.status;
      }
    }
    that.setData({
      bloodSugarDataList: bloodSugarList
    })
  },

  //记录
  lickBtn:function(){
    wx.navigateTo({
      url: '../record/record'
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBloodSugarData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})